package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.RoamingAreaAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@AttrHandler(RoamingAreaAttrHandler.class)
public class RoamingAreaAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String custId = data.getCustId();
        String sceneInstId = data.getSceneInstId();
		//APN标识
        KiteAttr relaApn = inst.getAttr("200004034");
        if (Objects.isNull(relaApn) || KiteStringUtils.isEmpty(relaApn.getValue())) {
            return;
        }
        Set<String> apns = new HashSet<>();
        Collections.addAll(apns, relaApn.getValue().split(","));
        //资费计划
        List<KiteProdInst> kiteProdInsts = querySceneInstService.listProdInsts(sceneInstId)
            .stream().filter(prodInst -> LocalKeyConsts.PROD_USE_TYPE_2000.equals(ProductCache.get(prodInst.getSpecId()).getProdUseType()))
            .collect(Collectors.toList());

        IOperateSceneInstService operateSceneInstService = SpringUtil.getBean(IOperateSceneInstService.class);
        kiteProdInsts.forEach(prodInst -> {
            //资费计划功能产品
            List<KiteProdInstFunc> prodInstFuncs = querySceneInstService.listProdInstFuncs(custId, prodInst.getInstId())
                .stream().filter(func -> KiteStringUtils.equalsAny(ProductCache.get(func.getProdId()).getProdNbr(), "300000171", LocalKeyConsts.FUNC_PROD_ID_YF5GZF_FW))
                .filter(func -> apns.contains(Optional.ofNullable(func.getAttr("200003728")).map(KiteAttr::getValue).orElse("")))
                .collect(Collectors.toList());
            String roamingAreaValue = attr.getValue();
            prodInstFuncs.forEach(func -> {
                KiteAttr roamingCountry = func.getAttr("200004028");
                //漫游区域选择国内和省内时,资费计划中的漫游国家隐藏
                if (Objects.nonNull(roamingCountry)) {
                    if (KiteStringUtils.equalsAny(roamingAreaValue, "1_0000223", "1_0000224")) {
                        roamingCountry.setIsNull(KeyConsts.IFTRUE_T);
                        roamingCountry.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    }
                    else {
                        roamingCountry.setIsNull(KeyConsts.IFTRUE_F);
                        roamingCountry.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    }
                    operateSceneInstService
                        .setAttr(data.getSceneInstId(), func.getInstType(), func.getInstId(), roamingCountry);
                }
            });
        });
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        // TODO Auto-generated method stub

    }

}
